/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_framework/zhiqim_orm.htm
 *
 * Zhiqim ORM is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.orm.dbo.defined;

import org.zhiqim.kernel.model.maps.LinkedMapSV;
import org.zhiqim.orm.ORMConstants;
import org.zhiqim.orm.dbo.Dbo;
import org.zhiqim.orm.dbo.DboField;


/**
 * 数据库参数或结果格式，配置格式如：<br><br>
 * <dbo name="com.zhiqim.data.User"><br>
 *     <field name="userId" column="USER_ID" type="long"/><br>
 *     <field name="userName" column="USER_NAME" type="String"/><br>
 * </dbo><br><br>
 * 用于ZSQL时根据column和type反射机制组装数据对象
 *
 * 注：_Dbo支持通过column找到_DboField
 * 
 * @version v1.0.0 @author zouzhigang 2014-3-21 新建与整理
 */
public class _Dbo implements ORMConstants, Dbo
{
    private String name;        //类名
    
    private LinkedMapSV<_DboField> fieldMap = new LinkedMapSV<>();
    
    public _Dbo(String name)
    {
        this.name = name;
    }
    
    public void addField(String name, String column, String type)
    {
        _DboField field = new _DboField(name, column, type);
        fieldMap.put(name, field);
    }
    
    @Override
    public String toString()
    {
        StringBuilder strb = new StringBuilder(_FOUR_).append("<dbo")
            .append(" name=").append(_DOUBLE_QUOTE_).append(name).append(_DOUBLE_QUOTE_)
            .append(">").append(_BR_);
        
        for (_DboField field : fieldMap.values())
        {
            strb.append(_FOUR_).append(_FOUR_).append(field).append(_BR_);
        }
        
        strb.append(_FOUR_).append("</dbo>");
        return strb.toString();
    }
    
    /***************************************************************************************************/
    //DBO接口实现的方法
    /***************************************************************************************************/
    
    @Override /** 获取类名 */
    public String getName()
    {
        return name;
    }
    
    @Override /** 判断是否存在该字段 */
    public boolean hasField(String fieldName)
    {
        return fieldMap.containsKey(fieldName)?true:getFieldByColumn(fieldName)!=null;
    }
    
    @Override /** 根据名称查字段 */
    public _DboField getField(String name)
    {
        _DboField field = fieldMap.get(name);
        if (field == null)
            field = getFieldByColumn(name);
        
        return field;
    }
    
    @Override /** 通过字段获取数据库列名 */
    public String getColumn(String fieldName)
    {
        _DboField field = getField(fieldName);
        return (field == null)?null:field.getColumn();
    }
    
    @Override /** 获取字段列表按长度排序 */
    public DboField[] getFieldColumnListOrderByLen()
    {
        _DboField[] array = new _DboField[fieldMap.size()];
        int index = 0;
        for (_DboField field : fieldMap.values())
        {
            array[index++] = field;
        }
        
        for (int i=0;i<array.length;i++)
        {
            for (int j=i+1;j<array.length;j++)
            {
                if (array[i].getField().length() < array[j].getField().length())
                {//冒泡交换
                    _DboField v = array[i];
                    array[i] = array[j];
                    array[j] = v;
                }
            }
        }
        
        return array;
    }
    
    /** 通过列名找到字段 */
    private _DboField getFieldByColumn(String column)
    {
        for (_DboField field : fieldMap.values())
        {
            if (field.getColumn().equals(column))
                return field;
        }
        
        return null;
    }
}